package com.light.gulimall.order.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

/**
 * 生产者发送消息指定order.create.order路由键发送到order-event-exchange交换机，
 * order-event-exchange交换机根据order.create.order路由键路由到order.delay.queue死信队列，
 * order.delay.queue死信队列在时间到后，再将消息指定order.release.order路由键放回order-event-exchange交换机，
 *  order-event-exchange交换机再根据order.release.order路由键将消息路由到order.release.order.queue
 *  消费者监听order.release.order.queue队列
 */
@Configuration
public class MyMQConfig {


    /**
     * 创建交换机
     * @return
     */
    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange", true, false);
    }

    /**
     * 定义死信队列，消息到达三十分钟后重新回到order-event-exchange交换机
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        HashMap<String, Object> map = new HashMap<>();
        //死信交换机
        map.put("x-dead-letter-exchange", "order-event-exchange");
        //路由键
        map.put("x-dead-letter-routing-key", "order.release.order");
        //过期时间
        map.put("x-message-ttl", 60000);
        return new Queue("order.delay.queue", true, false, false,map);
    }

    /**
     * 定义订单释放队列
     * @return
     */
    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue", true, false, false);
    }

    /**
     * 将order.delay.queue队列和order-event-exchange交换机绑定，路由键order.create.order
     * @return
     */
    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding("order.delay.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.create.order",
                null);
    }

    /**
     * 将order.release.order.queue队列和order-event-exchange交换机绑定，路由键order.release.order
     * @return
     */
    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding("order.release.order.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.order",
                null);
    }

    /**
     * 用于订单关单后，将消息通过order-event-exchange，路由键order.release.other.#，发送给stock.release.order.queue
     * @return
     */
    @Bean
    public Binding orderReleaseOtherOrderBinding() {
        return new Binding("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "order-event-exchange",
                "order.release.other.#",
                null
                );
    }

    /**
     * 秒杀队列
     * @return
     */
    @Bean
    public Queue orderSeckillOrderQueue() {
        return new Queue("order.seckill.order.queue", true, false, false);
    }

    /**
     * 秒杀队列和交换机绑定
     * @return
     */
    @Bean
    public Binding orderSeckillOrderBinding() {
        return new Binding("order.seckill.order.queue", Binding.DestinationType.QUEUE,"order-event-exchange",
                "order.seckill.order",null);
    }

}
